const {ccclass, property} = cc._decorator;

import remoteSensingHandle from '../controlCenter/remoteSensingHandle';

@ccclass
export default class tankPlayer extends cc.Component {

    // LIFE-CYCLE CALLBACKS:

    @property
    maxSpeed: number = 100;

    // 摇杆对象
    @property(remoteSensingHandle)
    remoteSensingHandle: remoteSensingHandle;
    // 当前坦克方向
    tankDirect: cc.Vec2 = cc.v2(0, 0);

    // 游戏摄像机
    @property(cc.Camera)
    camera: cc.Camera

    // 坦克的刚体组件
    private body: cc.RigidBody = null;
    onLoad () {
        this.body = this.node.getComponent(cc.RigidBody);
    }

    start () {
        if (this.camera) {
            this.camera.node.setPosition(this.node.getPosition());
        }
    }

    update (dt) {
        if (this.camera) {
            this.camera.node.setPosition(this.node.getPosition());
        }
        
        // 添加物理碰撞引擎后，不能通过改变坐标来使人物移动，需要通过改变线性速度
        let dir = this.remoteSensingHandle.direct;
        // 不移动
        if (dir.x === 0 && dir.y == 0) {
            this.body.linearVelocity = cc.v2(0, 0);
            // this.node.angle = 90;
            return ;
        } else {
            this.tankDirect.y = this.maxSpeed * dir.y;
            this.tankDirect.x = this.maxSpeed * dir.x;
            // console.log(dir);
            this.body.linearVelocity = this.tankDirect;

            let r:number = Math.atan2(dir.y, dir.x);
            let degree:number = r * 180 / Math.PI;
            // console.log(this.node.rotation);
            this.node.angle = degree;
        }
    }
}


